<template>
  <div class="container">
    <el-dialog
      title="绑定仓库"
      :visible.sync="dialog.centerDialogVisible"
      dialog
      width="70%"
      center
      :lock-scroll="true"
      @open="getData"
      @close="cancel"
    >
      <el-input
        style="width: 230px; margin: 0 10px"
        v-model="search.value"
        clearable
        placeholder="请输入仓库编码/仓库名称"
      ></el-input>

      <el-button
        size="small"
        type="primary"
        icon="el-icon-search"
        class="handleSearch"
        @click="handleSearch()"
        >查询</el-button
      >
      <div class="tagList">
        <el-tag
          v-for="tag in tags"
          :key="tag.name"
          closable
          @close="handleClose(tag)"
          >{{ tag.name }}</el-tag
        >
      </div>
      <el-table
        :data="tableData"
        max-height="450px"
        border
        style="margin-top: 10px"
        ref="multipleTable"
        empty-text="暂无数据"
      >
        <el-table-column
          prop="house_name"
          label="仓库名称"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="house_code"
          label="仓库编码"
          align="center"
        ></el-table-column>
        <el-table-column label="操作" width="150" align="center">
          <template slot-scope="scope">
            <el-button
              class="add"
              size="small"
              type="primary"
              icon="el-icon-circle-plus"
              @click="add(scope)"
              >绑定</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <pagination
        @page="handleSizeChange"
        @pagesize="handleCurrentChange"
        :pageData="pageData"
      ></pagination>
      <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="cancel">返 回</el-button>
        <!-- <el-button type="primary" @click="confirm()">确 定</el-button> -->
      </span>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from "@/components/pagination";
import { selectChooseWarehouse } from "@/api/warehouse/warehouse";
import { setWareHouse } from "@/api/select/select";
export default {
  props: {
    dialog: Object,
  },
  components: {
    pagination: Pagination,
  },
  data() {
    return {
      search: {
        value: "",
      },
      tags: [],
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },
    };
  },
  methods: {
    getData() {
      let data =
        "&name=" +
        this.search.value +
        "&limit=" +
        this.pageData.pageSize +
        "&page=" +
        this.pageData.page;
      selectChooseWarehouse(data).then((res) => {
        if (res.code == 0) {
          this.tableData = res.data.data;
          this.pageData.page = res.data.currentPage;
          this.pageData.total = res.data.total;
        }
      });
    },

    //添加
    add(scope) {
      console.log(this.dialog.id);
      console.log(scope, 123321);
      let data = {
        house_id: scope.row.id,
        id: this.dialog.id,
      };
      setWareHouse(data)
        .then((res) => {
          if (res.code == 0) {
            this.$message({
              message: res.msg,
              type: "success",
            });
            this.hideDialog();
            this.$emit("upData");
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },

    hideDialog() {
      this.dialog.centerDialogVisible = false;
    },
    //确定
    confirm() {
      //   this.$emit("tag", this.tags);
      this.cancel();
    },
    //取消
    cancel() {
      this.search.value = "";
      this.formData = [];
      this.tags = [];
      this.hideDialog();
    },
    //搜索
    handleSearch() {
      this.pageData.total = null;
      this.pageData.page = 1;
      this.getData();
    },

    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;

      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
  },
};
</script>
<style scoped>
.tagList {
  margin-top: 10px;
}
.el-tag {
  margin-right: 5px;
  margin-top: 2px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>